package com.suncnpap.system.controller;

import com.app.util.common.Constants;
import com.app.util.common.HandlerResult;
import com.app.util.common.ResponseUtil;
import com.app.util.common.UuidUtil;
import com.app.util.database.mybatis.Page;
import com.app.util.database.mybatis.QueryType;
import com.suncnpap.base.common.GlobaUser;
import com.suncnpap.base.common.ParamModel;
import com.suncnpap.base.controller.BaseController;
import com.suncnpap.reader.model.Reader;
import com.suncnpap.reader.service.ReaderService;
import com.suncnpap.system.model.Admin;
import com.suncnpap.system.service.AdminService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** 
 * 说明：管理员Controller
 */
@Controller
@RequestMapping(value="/system/admin")
public class AdminController extends BaseController {
	
	//private static final Logger log = Logger.getLogger(AdminController.class);
	
	@Autowired
	AdminService adminService;
	
	@Autowired
	ReaderService readerService;

	/**
	 * 列表页面数据处理，包括查询
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	public void list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		
		if (result.getCode()==0) {

			Page page = this.setPage();
			
//			page.removeQueryAnd("strSchoolId");
			
			page.addQueryAnd("strId", QueryType.markNOTEQ, GlobaUser.getStrId(request.getHeader("token")));
			
			String strUser=this.getParameter("strUser","");	//图书条码
			if(!"".equals(strUser)){
				page.addQueryAnd("strUser", QueryType.markLIKE, strUser);
			}
			
			String strName=this.getParameter("strName","");	//图书条码
			if(!"".equals(strName)){
				page.addQueryAnd("strName", QueryType.markLIKE, strName);
			}
			
			String strGroupTypeId=this.getParameter("strGroupTypeId","");	//图书条码
			if(!"".equals(strGroupTypeId)){
				page.addQueryAnd("strGroupTypeId", strGroupTypeId);
			}
			
			String strCollectionLocationId = this.getParameter("strCollectionLocationId", "");
			if(!strCollectionLocationId.equals("")){
				page.addQueryAnd("strCollectionLocationId",  strCollectionLocationId);
			}
			
			String strSchoolId = this.getParameter("strSchoolId", "");
			if(!strSchoolId.equals("")){
				page.addQueryAnd("strSchoolId", strSchoolId);
			}
			
			page = adminService.list(page);
			result = this.returnPage(result, page);
		}
		
		ResponseUtil.writeJson(result, response);

	}
	
	/**
	 * 添加处理
	 * @param obj
	 * @param response
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public void add(@RequestBody Admin obj, HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				Admin admin = adminService.findByUserId(obj.getStrUser(), obj.getStrSchoolId());
				if(admin==null){
					String strSchoolId = obj.getStrSchoolId();
					init(obj);
					obj.setStrSchoolId(strSchoolId);
//					obj.setStrId(this.get32UUID());
					obj.init();
					adminService.add(obj);
				}
				
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}

	/**
	 * 修改处理
	 * @param obj
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public void update(@RequestBody Admin obj,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				adminService.update(obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}

	/**
	 * 删除处理
	 * @param paramModel
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public void delete(@RequestBody ParamModel paramModel, HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				String strId = paramModel.getStrId();
				if(strId != null && !strId.equals("")){
					adminService.deleteMore(strId.split(","));
				}
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}

	/**
	 * 修改邮箱
	 * @param paramModel
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/updateEmail",method=RequestMethod.POST)
	public void updateEmail(@RequestBody ParamModel paramModel, HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if(result.getCode()==0){
			try{
				String strUser = paramModel.getStrUser();
				String strEmailaddr = paramModel.getStrEmailaddr();
				String strEmailPwd = paramModel.getStrEmailPwd();
				Admin obj = adminService.findById(strUser);
//				BeanUtil.copyProperty(obj, this.getRequest());		//对象赋值
				
				obj.setStrEmailaddr(strEmailaddr);
				obj.setStrEmailPwd(strEmailPwd);
				adminService.update(obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		
		ResponseUtil.writeJson(result, response);
	}
	
	@RequestMapping(value="/updatePassword",method=RequestMethod.POST)
	public void updatePassword(@RequestBody ParamModel paramModel,HttpServletRequest request, HttpServletResponse response)throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			String strSchoolId = String.valueOf(result.get("strSchoolId"));
			
			String strUser = paramModel.getStrUser();
			String oldStrPassWord = paramModel.getOldStrPassWord();
			String newStrPassWord = paramModel.getNewStrPassWord();
			String type = paramModel.getStrType();
			if(type.equals("admin")){
				Admin admin=adminService.findByUserId(strUser, strSchoolId);
				oldStrPassWord=new SimpleHash("MD5", oldStrPassWord,admin.getStrSalt()).toString();
				if(oldStrPassWord.equals(admin.getStrPassword())){
					String strSalt= UuidUtil.get32UUID();
					admin.setStrSalt(strSalt);
					admin.setStrPassword(new SimpleHash("MD5", newStrPassWord,strSalt).toString());
					adminService.update(admin);
				}else{
					result.setCode(1);
					result.setMessage("原密码错误");
				}
			}else{
				Reader reader= readerService.findByCardNumber(strUser, strSchoolId);
				oldStrPassWord=new SimpleHash("MD5", oldStrPassWord,reader.getStrSalt()).toString();
				if(oldStrPassWord.equals(reader.getStrPassword())){
					String strSalt= UuidUtil.get32UUID();
					reader.setStrSalt(strSalt);
					reader.setStrPassword(new SimpleHash("MD5", newStrPassWord,strSalt).toString());
					readerService.update(reader);
				}else{
					result.setCode(1);
					result.setMessage("原密码错误");
				}
			}
			
		}
		ResponseUtil.writeJson(result, response);
	}
	
	@RequestMapping(value="/resetPassword",method=RequestMethod.POST)
	public void resetPassword(@RequestBody ParamModel paramModel,HttpServletRequest request, HttpServletResponse response)throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			String strSchoolId = String.valueOf(result.get("strSchoolId"));
			
			String strId = paramModel.getStrId();
			String type = paramModel.getStrType();
			
			String strSalt = UuidUtil.get32UUID();
			String strPassword = new SimpleHash("MD5", Constants.DEFAULT_PWD,strSalt).toString();
			if(type.equals("admin")){
				adminService.updatePassword(strId.split(","), strPassword, strSalt);
			}else{
				readerService.updatePassword(strId.split(","), strPassword, strSalt);
			}
			
		}
		ResponseUtil.writeJson(result, response);
	}
	
	@RequestMapping(value="/view")
	public void view(@RequestBody ParamModel paramModel,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				String strId = paramModel.getStrId();
				Admin obj = adminService.findById(strId);
				result.put("obj", obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
}
